#ifndef LOGINSCENE_H_20120412
#define LOGINSCENE_H_20120412

#include "NetworkDelegate.h"
#include "cocos2d.h"
#include "InlineFunc.h"

#include "CocosUI/CocosUI.h"
#include <vector>

enum LoginID
{
	BTN_LOGIN_LOGIN,
	BTN_LOGIN_CREATEROOM,
	BTN_LOGIN_JOINROOM,
	BTN_LOGIN_REG,
	BTN_LOGIN_REGBACK,
	BTN_LOGIN_CREATEROLE,
	BTN_LOGIN_FIGHT,
	BTN_LOGIN_CREATEACCOUNT,
	BTN_LOGIN_LEAVEROOM,
	BTN_LOGIN_SET,
	INPUT_LOGIN_REGACCOUNT,
	INPUT_LOGIN_REGPSW,
	INPUT_LOGIN_REGPSWCONFIRM,
	INPUT_LOGIN_ACCOUNT,
	INPUT_LOGIN_PSW,
	BTN_REGION_BACK,
	BTN_LOGIN_REGION,
	

};

enum SpriteTag
{
	TAG_ACCOUNT = 1000,
	TAG_INPUT_ACCOUNT,
	TAG_INPUT_PSW,
	TAG_INPUT_REGACCOUNT,
	TAG_INPUT_REGPSW,
	TAG_INPUT_REGPSWCONFIRM,
	TAG_REGION_BACK,
	TAG_BTN_REGIONBACK,
	TAG_SPRITE_REGBACK
};
class CLoginScene : public cocos2d::CCLayer, public CAccountDelegate, public CGameDelegate, public cocos2d::CCIMEDelegate, CUIListener
{
public:
    CLoginScene()
        : m_chooseID(-1)
        , m_lastAdjustVertY(0)
        , m_regionCount(0)
    {
        m_pServer = CServerHelper::GetInstance();
        m_pServer->SetGameDelegate(this);
        m_pServer->SetAccountDelegate(this);
    }
    
    
    bool init();
	void onEnter();

	LAYER_NODE_FUNC(CLoginScene);
	LAYER_SHOW_FUNC(CLoginScene);

    virtual void onReceiveMsg(int aID, int event = 0, const void *pData = NULL);
    
	
	void OnShowAccount(cocos2d::ccTime dt);
	CCocosInputBox *BuildTextField(int id, const char *szTips);
	void CreateButtons();
	void CreateAccountLayer();
	void CreateRegisterLayer();
	void CreateChooseRegionLayer();

	void HideLogin();
	void ShowLogin();
	void HideRegion();
	void ShowRegion(std::vector<RegionInfo> *pRegionList);
	void HideReg();
	void ShowReg();
	void ChangeToSet();
	void ChangeToLogin();
	void LoginToRegion(std::vector<RegionInfo> *pRegionList);
	void RegionToLogin();
	void Tick(cocos2d::ccTime dt);

private:
	virtual void connectServerBack(bool bSuccess);
	virtual void userLoginBack(bool bSuccess, std::vector<RegionInfo > *pRegionList);
	virtual void userRegisterBack(bool bSuccess);
	virtual void chooseRegionBack(bool bSuccess, RegionUser *pUserInfo);
	virtual void accountServerLost();
	virtual void otherReceiveMsg(int id);
	virtual void otherAccountMsg(int id);
    
    // 返回连接区域信息
    virtual void connectRegionBack(bool bSuccess);
    // 返回登陆区域信息，如果成功，可以进行游戏了
    virtual void loginRegionBack(bool bSuccess);
    // 需要创建一个人物
    virtual void regionNoRole();
	virtual void recvChatFromName(const char *szName, const char *szMsg, int len);
    virtual void gameServerLost();

private:
	CServerHelper          *m_pServer;
	//cocos2d::CCLabelBMFont *m_pLable;
	//cocos2d::CCLayerColor  *m_pLayTip;
	CCocosTipBox           *m_pTipBox;
    
	cocos2d::CCLayer *m_pLayAccount;
	cocos2d::CCLayer *m_pLayRegister;
	cocos2d::CCLayer *m_pLayChooseRegion;

	cocos2d::CCLabelTTF     *m_pLabelMsg;
    
	cocos2d::CCNode         *m_pCurTrackNode;
    float                    m_lastAdjustVertY;
    int                      m_regionCount;

    std::vector<int>         m_vRegionIDs;

    
    CCocosButton            *m_pBtnLogin;
    CCocosButton            *m_pBtnCreateAccount;
	CCocosButton            *m_pBtnSet;
 
    int m_chooseID;

	std::string             m_strRegName;
	std::string             m_strRegPsw;
	bool                    m_bFromReg;

	float                   m_showTimer;
};
#endif